package com.example.controller;

import com.example.pojo.ForumReply;
import com.example.pojo.ForumTopic;
import com.example.pojo.User;
import com.example.service.ForumReplyService;
import com.example.service.ForumTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 二七和夭夭
 * @email 2646498606@qq.com
 * @date 2022/8/23 19:29
 */
@Controller
@RequestMapping("/reply")
public class ForumReplyController {
    @Autowired
    private ForumReplyService forumReplyService;
    @Autowired
    private ForumTopicService forumTopicService;

    /**
     * 前往添加页面
     * @param model
     * @param topicId
     * @return
     */
    @RequestMapping("/addUI.do")
    public String toAddUI(Model model,Integer topicId) {
        List<ForumTopic> topicList = forumTopicService.getForumTopics(topicId);
        ForumTopic topic = topicList.get(0);
        model.addAttribute("topic", topic);

        return "/jsp/reply/add.jsp";
    }

    /**
     * 添加回复
     * @return
     */
    @RequestMapping("/add.do")
    public String addReply(HttpServletRequest req,Integer topicId, Integer replyCount, Integer forumId,
                           Integer articleCount, String content) {
        //获取登录用户
        User user = (User) req.getSession().getAttribute("loginUser");
        //回复对象
        ForumReply forumReply = new ForumReply();
        forumReply.setTopicId(topicId);//主题id
        forumReply.setContent(content);//回复内容
        forumReply.setAuthorId(user.getUid());
        forumReply.setPostTime(new Date());//发布时间
        forumReply.setIpAddress("127.0.0.1");
        forumReply.setDeleted("0");

        boolean update = forumReplyService.addReply(forumReply,++replyCount,forumId,++articleCount);
        return "redirect:/topic/showReply.do?tid=" + topicId;
    }
}
